﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using TrainigWorkshop.Model;

namespace TrainigWorkshop.DAL_EF
{
    public class StudentRespository_EF : IStudentRespository
    { 
        public void InsertStudentInfo(StudentModel student)
        {
            using (projectbEntities edm = new projectbEntities())
            {
                Student stu  = ModelTranslation.StudentModelTranslator.ToStudentEF(student);
                edm.Students.AddObject(stu);
                edm.SaveChanges();
            }
        }
        public void UpdateStudentInfo(StudentModel student)
        {
            using (projectbEntities edm = new projectbEntities())
            {
                Student stu = edm.Students.SingleOrDefault(s => s.StudentId == student.StudentId);
                ModelTranslation.StudentModelTranslator.ToStudentEF(student, stu);               
                if (stu != null)
                {
                    edm.SaveChanges();
                }
            }         
        }
        public void DeleteStudentInfo(int id)
        {
            using (projectbEntities edm = new projectbEntities())
            {
                Student stu = (from s in edm.Students
                               where s.StudentId == id
                               select s).SingleOrDefault<Student>();
                edm.DeleteObject(stu);
                if (stu != null)
                {
                    edm.SaveChanges();
                }
            }
        }
        public StudentModel GetStudentInfoById(int id)
        {
            //1.get from EF
            Student stu;
            using (projectbEntities edm = new projectbEntities())
            { 
                stu  = (from s in edm.Students
                        where s.StudentId == id
                        select s).SingleOrDefault<Student>();
            }

            //2.translate
            StudentModel result = ModelTranslation.StudentEFTranslator.ToStudentModel(stu);
            return result;
        }

        public List<StudentModel> GetStudentInfo()
        {
            using (projectbEntities edm = new projectbEntities())
            {
                List<Student> students = (from s in edm.Students select s).ToList<Student>();
                foreach (Student obj in students)
                {
                    obj.ClassReference.Load();
                }
                List<StudentModel> studentCourseDetail = ModelTranslation.StudentEFTranslator.ToStudentModel(students);
                return studentCourseDetail;
            }
        }

       // public void LinqToEntitiesPage(int pageIndex,int pageSize)
       //{

       //    int skipCount = (pageIndex - 1) * pageSize;
       //   using (projectbEntities edm = new projectbEntities())
       //    {          
       //        var stdudent1 = (from s in edm.Students
       //                         orderby s.StudentId
       //                         select s).Skip(skipCount).Take(pageSize).ToList();     
                                         
       //   }
       //}


    }
}
